package com.example.beikeyigou;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.login.UserBean;
import com.example.beikeyigou.untils.SaveUserUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RadioGroup rg;
    Button fpagebtn,typebtn,mebtn,loginbtn,regbtn,cancelbtn;
    TextView logintv;//登陆提示文本
    DrawerLayout drawerLayout;
    UserBean.DataBean dataBean;

    private FragmentManager fm;
    List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();
        initFragment();

    }

    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new FPageFragment());
        fragmentList.add(new TypeFragment());
        fragmentList.add(new MeFragment());
        // 将碎片添加到activity
        // 1。获取碎片管理者对象
        fm = getSupportFragmentManager();
        // 2.获取碎片事物对象
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            transaction.add(R.id.main_layout,fragmentList.get(i));
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(0));
        transaction.commit();
    }

    // 切换Fragment
    private void changeFragment(int pos){
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            transaction.hide(fragmentList.get(i));
        }
        transaction.show(fragmentList.get(pos));
        transaction.commit();
    }

    private void setListener() {
        fpagebtn.setOnClickListener(onClickListener);
        typebtn.setOnClickListener(onClickListener);
        mebtn.setOnClickListener(onClickListener);
        loginbtn.setOnClickListener(onClickListener);
        regbtn.setOnClickListener(onClickListener);
        cancelbtn.setOnClickListener(onClickListener);
        rg.setOnCheckedChangeListener(listener);
    }


    //创建监听对象
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();  // 获取到被点击的id
            switch (id) {
                case R.id.drawer_btn_fpage:
                    rg.check(R.id.main_rb1);
                    break;
                case R.id.drawer_btn_type:
                    rg.check(R.id.main_rb2);
                    break;
                case R.id.drawer_btn_me:
                    rg.check(R.id.main_rb3);
                    break;
                case R.id.drawer_btn_login:
                    //判断是否登录
                    isLogin(0);
                    break;

                case R.id.drawer_btn_reg:
                    isLogin(1);
                    break;
                case R.id.drawer_btn_cancel:
                    exitLogin();
                    break;
            }
            drawerLayout.closeDrawers();  //关闭抽屉
        }
    };

    private void exitLogin(){
        UserBean.DataBean userinfo = SaveUserUtils.getUserInfo(this);
        if (userinfo == null){
            Toast.makeText(this,"您还未登录！",Toast.LENGTH_SHORT).show();

        }
        else{
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息");
            builder.setMessage("是否注销？");
            builder.setIcon(R.mipmap.icon);
            builder.setPositiveButton("取消",null);
            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //删除手机中的文件
                    SaveUserUtils.deleteUserINfo(MainActivity.this);
                    Toast.makeText(MainActivity.this,"注销成功",Toast.LENGTH_SHORT).show();
                    logintv.setText("未登录请先登录!");
                    Fragment fragment = fragmentList.get(2);
                    fragment.onResume();

                }
            });
            builder.create().show();


        }
    }


    //判断是否登录
    private boolean isLogin(int flag) {
        UserBean.DataBean userinfo = SaveUserUtils.getUserInfo(this);
        if (userinfo == null){
            Intent it1 = new Intent(MainActivity.this,LoginRegActivity.class);
            it1.putExtra("flag",flag);
            startActivity(it1);
            return false;
        }
        else {
            Intent intent = new Intent(this,PersonCenterActivity.class);
            intent.putExtra("flag",flag);
            startActivity(intent);
            return true;
        }

    }


    RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.main_rb1:
                    changeFragment(0);

                    break;
                case R.id.main_rb2:
                    changeFragment(1);
                    break;
                case R.id.main_rb3:
                    changeFragment(2);
                    break;
            }
        }
    };


    // 查找控件
    private void initView() {
        rg = findViewById(R.id.main_rg);
        fpagebtn = findViewById(R.id.drawer_btn_fpage);
        typebtn = findViewById(R.id.drawer_btn_type);
        mebtn = findViewById(R.id.drawer_btn_me);
        loginbtn = findViewById(R.id.drawer_btn_login);
        regbtn = findViewById(R.id.drawer_btn_reg);
        cancelbtn = findViewById(R.id.drawer_btn_cancel);
        logintv = findViewById(R.id.drawer_tv_name);
        drawerLayout = findViewById(R.id.main_drawer);

        dataBean = SaveUserUtils.getUserInfo(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setWelcomeText();
    }



    private void setWelcomeText() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        if (userInfo == null){
            logintv.setText("未登录请先登录!");
        }
        else {
            String nickName = userInfo.getNickname();
            String msg = nickName+"用户,欢迎登录使用";
            logintv.setText(msg);
        }


    }


}